<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Class program_cache</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Compute">
<link rel="up" href="../../boost_compute/reference.html#header.boost.compute.utility.program_cache_hpp" title="Header &lt;boost/compute/utility/program_cache.hpp&gt;">
<link rel="prev" href="../../BOOST_COMPUTE_DET_idm11668.html" title="Macro BOOST_COMPUTE_DETAIL_DEFINE_INVOKE">
<link rel="next" href="../../BOOST_COMPUTE_STR_idm11779.html" title="Macro BOOST_COMPUTE_STRINGIZE_SOURCE">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../BOOST_COMPUTE_DET_idm11668.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../boost_compute/reference.html#header.boost.compute.utility.program_cache_hpp"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../../BOOST_COMPUTE_STR_idm11779.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="refentry">
<a name="boost.compute.program_cache"></a><div class="titlepage"></div>
<div class="refnamediv">
<h2><span class="refentrytitle">Class program_cache</span></h2>
<p>boost::compute::program_cache</p>
</div>
<h2 xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv-title">Synopsis</h2>
<div xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv"><pre class="synopsis"><span class="comment">// In header: &lt;<a class="link" href="../../boost_compute/reference.html#header.boost.compute.utility.program_cache_hpp" title="Header &lt;boost/compute/utility/program_cache.hpp&gt;">boost/compute/utility/program_cache.hpp</a>&gt;

</span>
<span class="keyword">class</span> <a class="link" href="program_cache.html" title="Class program_cache">program_cache</a> <span class="special">:</span> <span class="keyword">private</span> <span class="identifier">noncopyable</span> <span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
  <span class="comment">// <a class="link" href="program_cache.html#boost.compute.program_cacheconstruct-copy-destruct">construct/copy/destruct</a></span>
  <a class="link" href="program_cache.html#idm11759-bb"><span class="identifier">program_cache</span></a><span class="special">(</span><span class="identifier">size_t</span><span class="special">)</span><span class="special">;</span>
  <a class="link" href="program_cache.html#idm11765-bb"><span class="special">~</span><span class="identifier">program_cache</span></a><span class="special">(</span><span class="special">)</span><span class="special">;</span>

  <span class="comment">// <a class="link" href="program_cache.html#idm11690-bb">public member functions</a></span>
  <span class="identifier">size_t</span> <a class="link" href="program_cache.html#idm11691-bb"><span class="identifier">size</span></a><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>
  <span class="identifier">size_t</span> <a class="link" href="program_cache.html#idm11694-bb"><span class="identifier">capacity</span></a><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>
  <span class="keyword">void</span> <a class="link" href="program_cache.html#idm11697-bb"><span class="identifier">clear</span></a><span class="special">(</span><span class="special">)</span><span class="special">;</span>
  <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span> <span class="identifier">program</span> <span class="special">&gt;</span> <a class="link" href="program_cache.html#idm11700-bb"><span class="identifier">get</span></a><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span><span class="special">)</span><span class="special">;</span>
  <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span> <span class="identifier">program</span> <span class="special">&gt;</span> <a class="link" href="program_cache.html#idm11708-bb"><span class="identifier">get</span></a><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span><span class="special">)</span><span class="special">;</span>
  <span class="keyword">void</span> <a class="link" href="program_cache.html#idm11720-bb"><span class="identifier">insert</span></a><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">program</span> <span class="special">&amp;</span><span class="special">)</span><span class="special">;</span>
  <span class="keyword">void</span> <a class="link" href="program_cache.html#idm11729-bb"><span class="identifier">insert</span></a><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">program</span> <span class="special">&amp;</span><span class="special">)</span><span class="special">;</span>
  <span class="identifier">program</span> <a class="link" href="program_cache.html#idm11741-bb"><span class="identifier">get_or_build</span></a><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span><span class="special">,</span> 
                       <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">context</span> <span class="special">&amp;</span><span class="special">)</span><span class="special">;</span>

  <span class="comment">// <a class="link" href="program_cache.html#idm11767-bb">public static functions</a></span>
  <span class="keyword">static</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <a class="link" href="program_cache.html" title="Class program_cache">program_cache</a> <span class="special">&gt;</span> <a class="link" href="program_cache.html#idm11768-bb"><span class="identifier">get_global_cache</span></a><span class="special">(</span><span class="keyword">const</span> <span class="identifier">context</span> <span class="special">&amp;</span><span class="special">)</span><span class="special">;</span>
<span class="special">}</span><span class="special">;</span></pre></div>
<div class="refsect1">
<a name="idm39050"></a><h2>Description</h2>
<p>The <a class="link" href="program_cache.html" title="Class program_cache">program_cache</a> class stores program objects in a LRU cache.</p>
<p>This class can be used to help mitigate the overhead of OpenCL's run-time kernel compilation model. Commonly used programs can be stored persistently in the cache and only compiled once on their first use.</p>
<p>Program objects are stored and retreived based on a user-defined cache key along with the options used to build the program (if any).</p>
<p>For example, to insert a program into the cache: </p>
<pre class="programlisting"><span class="identifier">cache</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="string">"foo"</span><span class="special">,</span> <span class="identifier">foo_program</span><span class="special">)</span><span class="special">;</span>
</pre>
<p>And to retreive the program later: </p>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">program</span><span class="special">&gt;</span> <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">cache</span><span class="special">.</span><span class="identifier">get</span><span class="special">(</span><span class="string">"foo"</span><span class="special">)</span><span class="special">;</span>
<span class="keyword">if</span><span class="special">(</span><span class="identifier">p</span><span class="special">)</span><span class="special">{</span>
    <span class="comment">// program found in cache</span>
<span class="special">}</span>
</pre>
<p><span class="bold"><strong>See Also:</strong></span></p>
<p>program </p>
<p>
</p>
<p>
</p>
<div class="refsect2">
<a name="idm39095"></a><h3>
<a name="boost.compute.program_cacheconstruct-copy-destruct"></a><code class="computeroutput">program_cache</code> 
        public
       construct/copy/destruct</h3>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
<pre class="literallayout"><a name="idm11759-bb"></a><span class="identifier">program_cache</span><span class="special">(</span><span class="identifier">size_t</span> capacity<span class="special">)</span><span class="special">;</span></pre>
<p>Creates a new program cache with space for <code class="computeroutput">capacity</code> number of program objects. </p>
</li>
<li class="listitem">
<pre class="literallayout"><a name="idm11765-bb"></a><span class="special">~</span><span class="identifier">program_cache</span><span class="special">(</span><span class="special">)</span><span class="special">;</span></pre>Destroys the program cache. </li>
</ol></div>
</div>
<div class="refsect2">
<a name="idm39120"></a><h3>
<a name="idm11690-bb"></a><code class="computeroutput">program_cache</code> public member functions</h3>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
<pre class="literallayout"><span class="identifier">size_t</span> <a name="idm11691-bb"></a><span class="identifier">size</span><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span></pre>Returns the number of program objects currently stored in the cache. </li>
<li class="listitem">
<pre class="literallayout"><span class="identifier">size_t</span> <a name="idm11694-bb"></a><span class="identifier">capacity</span><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span></pre>Returns the total capacity of the cache. </li>
<li class="listitem">
<pre class="literallayout"><span class="keyword">void</span> <a name="idm11697-bb"></a><span class="identifier">clear</span><span class="special">(</span><span class="special">)</span><span class="special">;</span></pre>Clears the program cache. </li>
<li class="listitem">
<pre class="literallayout"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span> <span class="identifier">program</span> <span class="special">&gt;</span> <a name="idm11700-bb"></a><span class="identifier">get</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span> key<span class="special">)</span><span class="special">;</span></pre>
<p>Returns the program object with <code class="computeroutput">key</code>. Returns a null optional if no program with <code class="computeroutput">key</code> exists in the cache. </p>
</li>
<li class="listitem">
<pre class="literallayout"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span> <span class="identifier">program</span> <span class="special">&gt;</span> 
<a name="idm11708-bb"></a><span class="identifier">get</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span> key<span class="special">,</span> <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span> options<span class="special">)</span><span class="special">;</span></pre>
<p>Returns the program object with <code class="computeroutput">key</code> and <code class="computeroutput">options</code>. Returns a null optional if no program with <code class="computeroutput">key</code> and <code class="computeroutput">options</code> exists in the cache. </p>
</li>
<li class="listitem">
<pre class="literallayout"><span class="keyword">void</span> <a name="idm11720-bb"></a><span class="identifier">insert</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span> key<span class="special">,</span> <span class="keyword">const</span> <span class="identifier">program</span> <span class="special">&amp;</span> program<span class="special">)</span><span class="special">;</span></pre>Inserts <code class="computeroutput">program</code> into the cache with <code class="computeroutput">key</code>. </li>
<li class="listitem">
<pre class="literallayout"><span class="keyword">void</span> <a name="idm11729-bb"></a><span class="identifier">insert</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span> key<span class="special">,</span> <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span> options<span class="special">,</span> 
            <span class="keyword">const</span> <span class="identifier">program</span> <span class="special">&amp;</span> program<span class="special">)</span><span class="special">;</span></pre>Inserts <code class="computeroutput">program</code> into the cache with <code class="computeroutput">key</code> and <code class="computeroutput">options</code>. </li>
<li class="listitem">
<pre class="literallayout"><span class="identifier">program</span> <a name="idm11741-bb"></a><span class="identifier">get_or_build</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span> key<span class="special">,</span> <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span> options<span class="special">,</span> 
                     <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span> source<span class="special">,</span> <span class="keyword">const</span> <span class="identifier">context</span> <span class="special">&amp;</span> context<span class="special">)</span><span class="special">;</span></pre>
<p>Loads the program with <code class="computeroutput">key</code> from the cache if it exists. Otherwise builds a new program with <code class="computeroutput">source</code> and <code class="computeroutput">options</code>, stores it in the cache, and returns it.</p>
<p>This is a convenience function to simplify the common pattern of attempting to load a program from the cache and, if not present, building the program from source and storing it in the cache.</p>
<p>Equivalent to: </p>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">program</span><span class="special">&gt;</span> <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">get</span><span class="special">(</span><span class="identifier">key</span><span class="special">,</span> <span class="identifier">options</span><span class="special">)</span><span class="special">;</span>
<span class="keyword">if</span><span class="special">(</span><span class="special">!</span><span class="identifier">p</span><span class="special">)</span><span class="special">{</span>
    <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">program</span><span class="special">::</span><span class="identifier">create_with_source</span><span class="special">(</span><span class="identifier">source</span><span class="special">,</span> <span class="identifier">context</span><span class="special">)</span><span class="special">;</span>
    <span class="identifier">p</span><span class="special">-&gt;</span><span class="identifier">build</span><span class="special">(</span><span class="identifier">options</span><span class="special">)</span><span class="special">;</span>
    <span class="identifier">insert</span><span class="special">(</span><span class="identifier">key</span><span class="special">,</span> <span class="identifier">options</span><span class="special">,</span> <span class="special">*</span><span class="identifier">p</span><span class="special">)</span><span class="special">;</span>
<span class="special">}</span>
<span class="keyword">return</span> <span class="special">*</span><span class="identifier">p</span><span class="special">;</span>
</pre>
<p> </p>
</li>
</ol></div>
</div>
<div class="refsect2">
<a name="idm39344"></a><h3>
<a name="idm11767-bb"></a><code class="computeroutput">program_cache</code> public static functions</h3>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
<pre class="literallayout"><span class="keyword">static</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <a class="link" href="program_cache.html" title="Class program_cache">program_cache</a> <span class="special">&gt;</span> 
<a name="idm11768-bb"></a><span class="identifier">get_global_cache</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">context</span> <span class="special">&amp;</span> context<span class="special">)</span><span class="special">;</span></pre>
<p>Returns the global program cache for <code class="computeroutput">context</code>.</p>
<p>This global cache is used internally by Boost.Compute to store compiled program objects used by its algorithms. All Boost.Compute programs are stored with a cache key beginning with <code class="computeroutput">"__boost"</code>. User programs should avoid using the same prefix in order to prevent collisions. </p>
</li></ol></div>
</div>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2013, 2014 Kyle Lutz<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../BOOST_COMPUTE_DET_idm11668.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../boost_compute/reference.html#header.boost.compute.utility.program_cache_hpp"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../../BOOST_COMPUTE_STR_idm11779.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
